/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/record', 'N/ui/dialog', 'N/search', 'N/https'], function (record, dialog, search, https) {
    var results;
    var processName;
    var processTypeId;
    var operationTaskId, operationsequence;
    var itemId;
    var detailRecItemId;
    var itemQuantity;
    var woId, wo, assemblyItemId, woUnits;
    var locationquantityavailable, locationquantityonhand;

    function pageInit(context) {
        try {
            woId = context.currentRecord.getValue('custpage_wo_num');
            wo = record.load({type: 'workorder', id: woId, isDynamic: true});
            assemblyItemId = wo.getValue('assemblyitem');
            woUnits = wo.getValue('units');
            var operationtaskSearch = search.create({
                type: 'manufacturingoperationtask',
                columns: ['manufacturingcosttemplate', 'name'],
                filters: [['workorder', 'is', woId]]
            });
            results = operationtaskSearch.run().getRange(0, 100);

            context.currentRecord.setValue('custpage_assembly', assemblyItemId);
            context.currentRecord.setValue('custpage_subsidiary', wo.getValue('subsidiary'));
            context.currentRecord.setValue('custpage_maxprocessnumber', results.length);
        } catch (e) {

        }
    }

    function validateField(context) {
        try {
            if (context.fieldId === 'custpage_woprocessseq') {
                var operationsequence = context.currentRecord.getValue('custpage_woprocessseq');
                if (isNaN(operationsequence)) {
                    showMsg('您只可在此字段内输入数字！');
                    clearAllFromOperationsequence(context);
                    context.currentRecord.setValue('custpage_woprocessseq', '');
                    return false;
                }
                if (operationsequence > results.length || operationsequence === '0' || operationsequence < 0) {
                    showMsg('请填写该工单正确的工序号：1~' + results.length);
                    window.location.reload();//工单东序号为0 刷新页面
                    clearAllFromOperationsequence(context);
                    context.currentRecord.setValue('custpage_woprocessseq', '');
                    return false;
                }
            }
        } catch (e) {
        }
        return true;
    }

    function fieldChanged(context) {
        try {
            if (context.fieldId === 'custpage_woprocessseq') {//工序号
                operationsequence = Number(context.currentRecord.getValue('custpage_woprocessseq'));
                getProcessByOperationsequence(operationsequence);
                context.currentRecord.setValue('custpage_woprocessname', processName);//设置工序名称
                context.currentRecord.setValue('custpage_woprocesstype', processTypeId);//设置工序类型
                // 设置收货地点
                if (processTypeId === '4') {//自制
                    context.currentRecord.setValue('custpage_receiptlocation', 1);//绍兴沐家线边仓
                } else if (processTypeId === '1' || processTypeId === '2') {//材料加工委外,材料深加工委外
                    context.currentRecord.setValue('custpage_receiptlocation', 13);//绍兴沐家面料加工委外仓
                } else if (processTypeId === '3') {//工序委外
                    context.currentRecord.setValue('custpage_receiptlocation', 12);//绍兴沐家半成品加工委外仓
                }
                //清空货品明细行
                var line = context.currentRecord.getLineCount('custpage_sublist_items');
                for (var i = line - 1; i >= 0; i--) {
                    context.currentRecord.removeLine('custpage_sublist_items', i);
                }
                //设置货品明细行
                for (var i = 0; i < wo.getLineCount('item'); i++) {
                    var operationsequencenumber = wo.getSublistValue('item', 'operationsequencenumber', i);
                    if (operationsequence === operationsequencenumber) {
                        itemId = wo.getSublistValue('item', 'item', i);
                        var itemName = wo.getSublistText('item', 'item', i);
                        //非费用货品 “工序加工费”,“材料加工费”,“材料深加工费” id 分别为868 ,49 ,866
                        if (itemId !== '868' && itemId !== '49' && itemId !== '866') {
                            context.currentRecord.selectNewLine('custpage_sublist_items');//选择新行
                            context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_item', itemId);//设置 非费用货品
                            var bomQuantity = wo.getSublistValue('item', 'bomquantity', i);
                            itemQuantity = wo.getSublistValue('item', 'quantity', i);
                            context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_quantity', bomQuantity);
                            context.currentRecord.commitLine('custpage_sublist_items');
                        }
                    }
                }
            }


            if (context.fieldId === 'custpage_from_location') {
                var itemId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_item');
                var locationId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_from_location');
                if (locationId === '') {
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_available_quantity', '');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_onhand_quantity', '');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_transfer_price', '');
                    return ;
                }
                var receptLocation = context.currentRecord.getValue('custpage_receiptlocation');
                if (locationId == receptLocation) {
                    showMsg('起始地点和收货地点不能一致，请选择！');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_from_location', '');
                    return;
                }
                if (itemId != '') {
                    var results = search.create({
                        type: 'item',
                        columns: ['locationquantityavailable', 'locationquantityonhand', 'averagecost'],
                        filters: [['internalid', 'is', itemId],
                            'and', ['inventorylocation', 'is', locationId]]
                    }).run().getRange(0, 1);
                    if (results.length > 0) {
                        locationquantityavailable = results[0].getValue('locationquantityavailable');
                        if (locationquantityavailable == '' || Number(locationquantityavailable) == 0) {
                            showMsg('此货品在此仓库可用量为0，无法发料！');
                            context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_from_location', '');
                            return;
                        }
                        locationquantityonhand = results[0].getValue('locationquantityonhand');
                        var averagecost = results[0].getValue('averagecost');

                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_available_quantity', locationquantityavailable);
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_onhand_quantity', locationquantityonhand);
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_transfer_price', averagecost);
                    } else {
                        showMsg('没有对应的仓库！');
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_from_location', '');
                        return;
                    }
                    //判断起始地点是否含有库位
                    results = search.create({
                        type: 'bin',
                        filters: [['location', 'is', locationId]]
                    }).run().getRange(0, 1);
                    if (results.length > 0) {
                        alert('请选择起始库位！');
                    } else {
                        alert('此起始地点不含起始库位，无需选择！');
                    }
                }

                //筛选出库位
                var binSearch = search.create({
                    type:record.Type.BIN,
                    columns:['binnumber','location'],
                    filters:[
                        ['location','is',locationId]
                    ]
                });
                var binField = context.getField('custpage_from_bin');
                binSearch.run().each(function(result){
                    binField.addSelectOption({
                        value : result.id,
                        text : result.getValue('binnumber')
                    });
                    return true;
                });


            }

            if (context.fieldId === 'custpage_from_bin') {
                var fromBinId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_from_bin');
                var fromBinName = context.currentRecord.getCurrentSublistText('custpage_sublist_items', 'custpage_from_bin');
                if (locationId === '' || fromBinId === '') {
                    return;
                }
                var itemId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_item');
                var locationId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_from_location');
                var results = search.create({
                    type: 'bin',
                    columns: ['location'],
                    filters: [['internalid', 'is', fromBinId]]
                }).run().getRange(0, 1);
                if (results[0].getValue('location') != locationId) {
                    showMsg('此起始库位不属于起始仓库！');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_from_bin', '');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_available_quantity', locationquantityavailable);
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_onhand_quantity', locationquantityonhand);
                    return;
                }
                results = search.create({
                    type: 'item',
                    columns: ['binonhandcount', 'binonhandavail'],
                    filters: [['internalid', 'is', itemId],
                        'and', ['binnumber', 'is', fromBinName]]
                }).run().getRange(0, 1);
                if (results.length <= 0) {
                    showMsg('该货品未维护此库位！');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_from_bin', '');
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_available_quantity', locationquantityavailable);
                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_onhand_quantity', locationquantityonhand);
                    return;
                }
                var binonhandcount = results[0].getValue('binonhandcount');
                var binonhandavail = results[0].getValue('binonhandavail');
                context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_onhand_quantity', binonhandcount);
                context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_available_quantity', binonhandavail);
            }

            if (context.fieldId === 'custpage_receipt_bin') {
                var binId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin');
                if (binId == '') {
                    return;
                }
                var itemId = context.currentRecord.getCurrentSublistValue('custpage_sublist_items', 'custpage_item');
                var binName = context.currentRecord.getCurrentSublistText('custpage_sublist_items', 'custpage_receipt_bin');


                if (processTypeId === '1' || processTypeId === '2' || processTypeId === '3') {//外协工序
                    var results = search.create({
                        type: 'bin',
                        columns: ['custrecord_supplier'],
                        filters: [['internalid', 'is', binId]]
                    }).run().getRange(0, 1);
                    //库位对应供应商
                    var vendorId = results[0].getValue('custrecord_supplier');
                    for (var i = 0; i < wo.getLineCount('item'); i++) {
                        var operationsequencenumber = wo.getSublistText('item', 'operationsequencenumber', i);
                        var woItemId = wo.getSublistValue('item', 'item', i);
                        if (operationsequencenumber == operationsequence) {
                            if (woItemId == '868' || woItemId == '49' || woItemId == '866') {
                                var poId = wo.getSublistValue('item', 'poid', i);
                                var results = search.create({
                                    type: 'purchaseorder',
                                    columns: ['entity'],
                                    filters: [['internalid', 'is', poId]]
                                }).run().getRange(0, 1);
                                //采购单对应供应商
                                var poVendorId = results[0].getValue('entity');
                                if (vendorId != poVendorId) {
                                    alert('库位选择不正确，请选择！');
                                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin', '');
                                    context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin_available', '');
                                    return;
                                }
                                break;
                            }
                        }
                    }
                }
                if (processTypeId === '4') {//自制工序

                    if (Number(binId) !== 3) {
                        alert('库位选择不正确，请选择！');
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin', '');
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin_available', '');
                        return;
                    }
                }
                if (itemId != '' && binName != '') {
                    var results = search.create({
                        type: 'item',
                        columns: ['binonhandavail'],
                        filters: [['internalid', 'is', itemId],
                            'and', ['binnumber', 'is', binName]]
                    }).run().getRange(0, 1);
                    console.log('results: ' + JSON.stringify(results));
                    if (results.length > 0) {
                        var binonhandavail = results[0].getValue('binonhandavail');
                        console.log('binonhandavail: ' + binonhandavail);
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin_available', binonhandavail);
                    } else {
                        alert('该货品没有对应此库位，请维护！');
                        context.currentRecord.setCurrentSublistText('custpage_sublist_items', 'custpage_receipt_bin', '');
                        context.currentRecord.setCurrentSublistValue('custpage_sublist_items', 'custpage_receipt_bin_available', '');
                        return;
                    }
                }
            }
        } catch (e) {
            console.log('e: ' + JSON.stringify(e));
        }
    }

    function saveRecord(context) {
        try {
            if (!context.currentRecord.getText('custpage_woprocessseq')) {
                alert('工序号为空！');
                return false;
            }
            if (context.currentRecord.getLineCount('custpage_sublist_items') <= 0) {
                alert('工序对应组件列表为空！');
                return false;
            } else {
                for (var i = 0; i < context.currentRecord.getLineCount('custpage_sublist_items'); i++) {
                    var itemId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_item', i);
                    if (itemId == '') {
                        alert('请完成第' + (i + 1) + '行货品的填写！');
                        return false;
                    }
                    var locationId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_from_location', i);
                    if (locationId == '') {
                        alert('请完成第' + (i + 1) + '行起始地点的填写！');
                        return false;
                    }
                    //判断起始地点是否有库位，若有则必填
                    var fromBin = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_from_bin', i);
                    if (fromBin == '') {
                        results = search.create({
                            type: 'bin',
                            filters: [['location', 'is', locationId]]
                        }).run().getRange(0, 1);
                        if (results.length > 0) {
                            alert('请完成第' + (i + 1) + '行起始库位的填写！');
                            return false;
                        }
                    }
                    var quantity = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i);
                    if (quantity == '') {
                        alert('请完成第' + (i + 1) + '行数量的填写！');
                        return false;
                    }
                    var binId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_receipt_bin', i);
                    if (binId == '') {
                        alert('请完成第' + (i + 1) + '行收货库位的填写！');
                        return false;
                    }
                }
            }
            var param = getCreateRecordParam(context);
            var response = https.get({url: '/app/site/hosting/scriptlet.nl?script=126&deploy=1&param=' + param});
            var messages = JSON.parse(response.body).messages;
            var count = 0;
            for (var i = 0; i < messages.length; i++) {
                alert(messages[i].msg);
                if (messages[i].success) {
                    count++;
                }
            }
            //创建的多个调拨单/库存转移单都已成功
            if (count == messages.length) {
                var result = createOrUpdateProcessDetailRecord(context);
                //如果更新失败的话，删除之前创建的调拨单或者库存转移单
                if (!result) {
                    for (var i = 0; i < messages.length; i++) {
                        record.delete({
                            type: messages[i].type,
                            id: messages[i].id
                        });
                    }
                    alert('调拨单或者库存转移单已删除，请重新发料！');
                }
                window.location.href = '/app/site/hosting/scriptlet.nl?script=118&deploy=1&custpage_wo_num=' + woId;
                return;
            } else {
                for (var i = 0; i < messages.length; i++) {
                    record.delete({
                        type: messages[i].type,
                        id: messages[i].id
                    });
                }
                return false;
            }
        } catch (e) {
            console.log(JSON.stringify(e));
        }
        return true;
    }

    function clearAllFromOperationsequence(context) {
        //清空工序名称、工序类型、收货地点
        context.currentRecord.setValue('custpage_woprocessname', '');
        context.currentRecord.setValue('custpage_woprocesstype', '');
        context.currentRecord.setValue('custpage_receiptlocation', '');
        //清空明细行
        var line = context.currentRecord.getLineCount('custpage_sublist_items');
        for (var i = line - 1; i >= 0; i--) {
            context.currentRecord.removeLine('custpage_sublist_items', i);
        }

    }

    //制造成本模版（获取工序类型）
    function getProcessByOperationsequence(operationsequence) {

        var manufacturingcosttemplateId = results[operationsequence - 1].getValue("manufacturingcosttemplate");
        processName = results[operationsequence - 1].getValue("name");
        operationTaskId = results[operationsequence - 1].id;
        var fieldLookUp = search.lookupFields({
            type: 'manufacturingcosttemplate',
            id: manufacturingcosttemplateId,
            columns: ['custrecord_processtype']
        });
        processTypeId = fieldLookUp.custrecord_processtype[0].value;
    }


    function getCreateRecordParam(context) {
        var param = {};
        if (processTypeId === '1' || processTypeId === '2' || processTypeId === '3') {//外协工序
            param.type = 'transferorder';//调拨单
            param.orderstatus = 'Pending Approval';
            param.toLocation = context.currentRecord.getValue('custpage_receiptlocation');
            param.subsidiary = 1; //绍兴沐家家居科技股份有限公司 1
            param.related = context.currentRecord.getValue('custpage_wo_num');
            param.items = [];
            var fromLocations = [];
            for (var i = 0; i < context.currentRecord.getLineCount('custpage_sublist_items'); i++) {
                var item = {};
                //有多个起始地点时，需要创建多个相应的调拨单
                var fromLocation = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_from_location', i);
                if (fromLocations.indexOf(fromLocation) < 0) {
                    fromLocations.push(fromLocation);
                }
                item.fromLocation = fromLocation;
                item.fromLocationName = context.currentRecord.getSublistText('custpage_sublist_items', 'custpage_from_location', i);
                item.itemId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_item', i);
                item.quantity = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i);
                item.rate = Number(context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_transfer_price', i));
                item.custcol_outsource_location = context.currentRecord.getValue('custpage_receiptlocation');
                item.custcol_outsource_bin = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_receipt_bin', i);
                item.custcol_outsourcelocationavailableqty = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_receipt_bin_available', i);
                param.items.push(item);
            }
            param.fromLocations = fromLocations;
        }
        if (processTypeId === '4') {//自制工序
            param.type = 'inventorytransfer';//库存转移单
            param.subsidiary = 1;  //绍兴沐家家居科技股份有限公司 1
            param.toLocation = 1;//绍兴沐家线边仓 1
            param.related = context.currentRecord.getValue('custpage_wo_num');
            param.items = [];
            var fromLocations = [];
            for (var i = 0; i < context.currentRecord.getLineCount('custpage_sublist_items'); i++) {
                var item = {};
                //有多个起始地点时，需要创建多个相应的库存转移单
                var fromLocation = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_from_location', i);
                if (fromLocations.indexOf(fromLocation) < 0) {
                    fromLocations.push(fromLocation);
                }
                item.fromLocation = fromLocation;
                item.fromLocationName = context.currentRecord.getSublistText('custpage_sublist_items', 'custpage_from_location', i);
                item.fromBin = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_from_bin', i);
                item.toBin = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_receipt_bin', i);
                item.itemId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_item', i);
                item.quantity = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i);
                param.items.push(item);
            }
            param.fromLocations = fromLocations;
        }
        param = JSON.stringify(param);
        return param;
    }


    function createOrUpdateProcessDetailRecord(context) {
        var detailId;
        search.create({
            type: 'customrecord_workorder_process_detail',
            columns: [],
            filters: [['custrecord_workordernum', 'is', woId],
                'and', ['custrecord_processnum', 'equalto', Number(operationsequence)],
                'and', ['custrecord_assembly', 'is', assemblyItemId]]
        }).run().each(function (result) {
            detailId = result.id;
        });
        console.log('detailId: ' + detailId);
        try {
            if (!detailId) { //不存在,则创建工单工序详情,创建工单工序详情列表
                var detailRec = record.create({type: 'customrecord_workorder_process_detail', isDynamic: true});
                detailRec.setValue('custrecord_workordernum', woId);//工单号
                detailRec.setValue('custrecord_processnum', Number(operationsequence));//工序号
                detailRec.setValue('custrecord_assembly', assemblyItemId);//装配品
                detailRec.setValue('custrecord_externalprocess', operationTaskId);//工序
                detailRec.setValue('custrecord_process_type', processTypeId);//工序类型
                detailRec.setValue('custrecord_workunit', woUnits);//单位
                var detailRecId = detailRec.save();
                for (var i = 0; i < context.currentRecord.getLineCount('custpage_sublist_items'); i++) {
                    var detailItemRec = record.create({type: 'customrecord_workorderitemdetails', isDynamic: true});
                    var detailRecItemId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_item', i);
                    var releaseQty = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i);
                    var results = search.create({
                        type: 'item',
                        columns: ['stockunit'],
                        filters: [['internalid', 'is', detailRecItemId]]
                    }).run().getRange(0, 1);
                    var itemUnits = results[0].getValue('stockunit');
                    detailItemRec.setValue('custrecord_workorderprocess', detailRecId);//关联工单工序详情
                    detailItemRec.setValue('custrecord_item', detailRecItemId);//itemId
                    detailItemRec.setValue('custrecord_releasequantity', releaseQty);//发料数量
                    detailItemRec.setValue('custrecord_unit', itemUnits);//item单位
                    detailItemRec.save();
                }
                alert('创建工单工序详情成功！id：' + detailRecId);
            }
            if (detailId) {
                var detailItemId;
                //存在 工单工序详情,则不创建工单工序详情,判断更新 工单工序详情货品明细行
                for (var i = 0; i < context.currentRecord.getLineCount('custpage_sublist_items'); i++) {
                    var detailRecItemId = context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_item', i);
                    search.create({ //search  相关的detailItemId
                        type: 'customrecord_workorderitemdetails',
                        columns: [],
                        filters: [['custrecord_workorderprocess', 'is', detailId],
                            'and', ['custrecord_item', 'is', detailRecItemId]]
                    }).run().each(function (result) {
                        detailItemId = result.id;
                    });
                    //工单工序详情  货品行 , 如果存在则  发料数量相加 , 不存在则创建
                    if (detailItemId) {
                        detailItemRec = record.load({
                            type: 'customrecord_workorderitemdetails',
                            id: detailItemId,
                            isDynamic: true
                        });
                        var releasequantity = Number(context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i));
                        var oldreleasequantity = Number(detailItemRec.getValue('custrecord_releasequantity'));
                        //设置发料数量
                        detailItemRec.setValue('custrecord_releasequantity', releasequantity + oldreleasequantity);
                        detailItemRec.save();
                    } else {
                        //工单工序详情  货品行
                        var detailItemRec = record.create({type: 'customrecord_workorderitemdetails', isDynamic: true});
                        var releaseQty = Number(context.currentRecord.getSublistValue('custpage_sublist_items', 'custpage_quantity', i));
                        var results = search.create({
                            type: 'item',
                            columns: ['stockunit'],
                            filters: [['internalid', 'is', detailRecItemId]]
                        }).run().getRange(0, 1);
                        var itemUnits = results[0].getValue('stockunit');
                        detailItemRec.setValue('custrecord_workorderprocess', detailId);//关联工单工序详情
                        detailItemRec.setValue('custrecord_item', detailRecItemId);//itemId
                        detailItemRec.setValue('custrecord_releasequantity', releaseQty);//发料数量
                        detailItemRec.setValue('custrecord_unit', itemUnits);//item单位
                        detailItemRec.save();
                    }
                }
                alert('更新工单工序详情成功！id：' + detailId);
            }
            return true;
        } catch (e) {
            alert('创建/更新工单工序详情失败！');
            console.log('e: ' + JSON.stringify(e));
            return false;
        }
    }

    function showMsg(msg){
        dialog.alert({
            title:'错误提示',
            message:msg
        });
    }


    return {
        pageInit: pageInit,
        validateField: validateField,
        fieldChanged: fieldChanged,
        validateLine:validateLine,
        saveRecord: saveRecord
    };
});